import { Dispatcher } from "../tools/Dispatcher";
import { GameCtrl } from "./GameCtrl";
import DispatcherEvent from "../tools/DispatcherEvent";

/**
 * @author seacole 
 * 鼠标事件
 */
export default class TouchCtrl {
    private static _instance: TouchCtrl;
    public static get instance(): TouchCtrl {
        if (!TouchCtrl._instance)
            TouchCtrl._instance = new TouchCtrl();
        return TouchCtrl._instance;
    }

    private _touchSp: Laya.Sprite;

    public init(touchSp: Laya.Sprite) {
        this._touchSp = touchSp;
        Dispatcher.addListener(DispatcherEvent.CAN_TOUCH, this, this.onCanTouch);
        // this.addListener();
    }

    private onCanTouch(canTouch: boolean): void {
        if (canTouch) {
            this.addListener();
        }
        else {
            this.removeListener();
        }
    }

    private _isMouseDown: boolean;
    public get isMouseDown(): boolean {
        return this._isMouseDown;
    }

    // private _mouseUpTime: number;
    // public get mouseUpTime(): number {
    //     return this._mouseUpTime;
    // }

    // private onStartGame(): void {
    //     this.addListener();
    // }

    // private onGameOver(): void {
    //     this._nowTouchId = -1;
    //     this._isMouseDown = false;
    //     this.removeListener();
    // }

    private addListener(): void {
        this._nowTouchId = -1;
        this._touchSp.on(Laya.Event.MOUSE_DOWN, this, this.onMouseEvent);
        this._touchSp.on(Laya.Event.MOUSE_UP, this, this.onMouseEvent);
        this._touchSp.on(Laya.Event.MOUSE_OUT, this, this.onMouseEvent);
        this._touchSp.on(Laya.Event.MOUSE_MOVE, this, this.onMouseEvent);
    }

    private removeListener(): void {
        this._touchSp.off(Laya.Event.MOUSE_DOWN, this, this.onMouseEvent);
        this._touchSp.off(Laya.Event.MOUSE_UP, this, this.onMouseEvent);
        this._touchSp.off(Laya.Event.MOUSE_OUT, this, this.onMouseEvent);
        this._touchSp.off(Laya.Event.MOUSE_MOVE, this, this.onMouseEvent);
        // Laya.timer.clear(this, this.onFrame);
    }

    private _nowTouchId: number;
    private onMouseEvent(e: Laya.Event): void {
        switch (e.type) {
            case Laya.Event.MOUSE_DOWN:
                if (this._nowTouchId == -1) {
                    this._nowTouchId = e.touchId;
                    this._isMouseDown = true;
                    GameCtrl.instance.mouseDown(e.stageX, e.stageY);
                }
                break;
            case Laya.Event.MOUSE_UP:
            case Laya.Event.MOUSE_OUT:
                if (e.touchId == this._nowTouchId) {
                    if (this._isMouseDown) {
                        this._nowTouchId = -1;
                        this._isMouseDown = false;
                        GameCtrl.instance.mouseUp(e.stageX, e.stageY);
                    }
                }
                break;

            case Laya.Event.MOUSE_MOVE:
                if (e.touchId == this._nowTouchId) {
                    if (this._isMouseDown) {
                        // let angle= Math.atan2(e.stageX-this.mouseX,e.stageY-this.mouseY)
						// console.log(angle)
                        GameCtrl.instance.mouseMove(e.stageX, e.stageY);
                    }
                }
                break;


        }
    }

}
